package com.gdut.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.gdut.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000117673725";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCMmS2HpSYuonZRSt9Z4n63hb/lyjFJSGK0SOG02AQ8+J1pz08f8r5SQErg+d5IdWzpf+Vk/XAWzYBYCXziqjHKokBufHa0QGGoYnI8DEeWP33YOLaukmzp/E5vEfCOV/ubAi6+oX3asySx5M5o0dE+Zf5QrAJzgZPZEzlxDSgvBJYwd1pE8YonMwCTZtub0ycvG5W0LQHalqTLP9K44nXSDAlbMm3Gasc56GdkFKUi/+YEkmvUMfN8BejzRt+VxG6WXII9VxA8rqJZRpel0VwC5/RIN8bGaS2fC7JafcQ7psSY2duJgzBM1n6mNbnctNBbJEt5AuhbUtsgPwSO51vxAgMBAAECggEAYlVPrqsmxT8DGWAQTkFG9vwodoKvbd2eixDeKylZ7XrEPZiscl/IFKa6uBaKDIt0HOtCeRt4JbqcpPYdzjnSkTJhdeS+8NqGcspHsw+zkbXeQjeILRhj7FJ2WOBnKmcGpickolc4OplIdNLeOhllCwsyjyF+pWJfEVeMuTDGhfrRaQkql/j54snsKSQ0POe3SFhW4N5/o4i+wQ6Spsz8L/qKfVPpkRuVlwARTAaImMMGsNh2GaUNkETUqq9qxHJXIASx0AmVRSY/gb6JW3v1pIHWiiwXDf3wUguPWKEyQhBXi0oen8Qe1z1wxQsD5jbgVGKidP8+RZjmlupomsCisQKBgQD0DhCrwqxtG4/Mtg3cDu02W41al0exUZbn4oexCGUUsUGmePCBHxF9uLpRzp8AqfXcnrR7dneTc3/xK/DkXNOy0bDDPoDDHbUutwSHTapdWyyLcHSZkzZQC4BpeOIT2aBp4+tLi51gDOfHMFyDezcgaa7q0M64CiRHzbjQyz/L7wKBgQCTetUzyTnSNL7BG0u+G9r+KdB/ShU7bzGLZHS7XLcAUjy7zMClYbqy9bLrXBcMmxxQdr0gaKhC6o0iS50VRbDMG7uetZX/ePE/wlmX6uwTle34sYAxXbUH464gzSQmrhojmEJ8MAO2fqQIQIuS6IvWQuU+NcQnthlC9uwqwSr2HwKBgD4gUngWz2Pa70kyUubg2dvLhHk6J2LpHRwcW2RiebDByq+A++Ays3Yojy2CBpHZsh+eOt1u89OSRFB1WhYX10AfBvs8rvR8XIHuzvVOfP7U1csNLnFlux63PtIalpps/NvXzC7n9IDd6CaZlgNWW1sh7AgUbXsGAeRRZymng677AoGAUB7nPN8RPpXRG+/MUlenvXTSBJUmu8dIz0Wf9ToFYXNL82rlLfPODfNVZG//RaXu1/fp/3VcFB6A1iTmDexyhDB82VglSUlXJyBatCG4Z1uIkRc9YZRbCRI0QzWvciQbp0LMu9MJ8VcTnjgSm5kifyCQkh97lWdkifDDlOakhFkCgYBhkGbK7GAdr68Tp8q6FqLFI6SHnHb9d8sKX/kaBkHv7fdTmLt3veDt62oXL3i1x+cjQfb/ht6cuUe5ZnZbDsiBhcSXsmg765sgG4EpXTnQAbjW1jaCDj0cbf5OPAwkgH0WLC7SPXalPCp37tGpZ5dEcDBbNUw5jowgWjnB1BpBKA==";
    
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqyo+6Sv42F0DXOlWlwvpLaIujzAqrVyxM3AUi7GnhjK9zapdWlrpNAblFjJQCSDH9y9c4XSyXqZheg/qLCE1ZNIcJ04WxqLO9bd48XqupGktFGK8OCREBGXIcx+8HxvB4pXXpEklPRLYUli5m3uOZSnmcNh+2wMh2mnivDTuxKhWyqAzWeJrIVVfkZjEePpeejLdg06jMqQuwdUSk2S9vd46WYokQ7iydwsvvKSd0hQntfhR+MYdaguvdT0NkDO6iUAslsJ4JfTW9DeaHRFzyZh4NxzC2s67kZayFMHRIdiv5/nPWFSKWnmuWmZiroU4qjerpvvF1GA857Nvm1/6twIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://krenfw.natappfree.cc/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url  = "http://member.gulimall.com/memberOrder.html";


    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
